using System;
using System.Collections.Generic;
using System.Text;

using System.Management.Automation;

namespace Microsoft.PowerShell.CmdletManagement
{
    [Cmdlet(VerbsCommon.Get, "PsSpecHistory")]
    public class GetPsSpecHistoryCommand : PsSpecCommandBase
    {
        [Parameter(Mandatory = true, Position = 0, ValueFromPipelineByPropertyName = true)]
        public int Id
        {
            get
            {
                return id;
            }
            set
            {
                id = value;
            }
        }
        private int id;

        [Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true)]
        [ValidateSet("Project", "Cmdlet")]
        public PsSpecHistoryChangeType ChangeType
        {
            get
            {
                return changeType;
            }
            set
            {
                changeType = value;
            }
        }
        private PsSpecHistoryChangeType changeType;

        [Parameter(Mandatory = false, Position = 2, ValueFromPipelineByPropertyName = true)]
        public DateTime From
        {
            get
            {
                return from;
            }
            set
            {
                from = value;
            }
        }
        private DateTime from = DateTime.MinValue;

        [Parameter(Mandatory = false, Position = 3, ValueFromPipelineByPropertyName = true)]
        public DateTime To
        {
            get
            {
                return to;
            }
            set
            {
                to = value;
            }
        }
        private DateTime to = DateTime.MaxValue;

        protected override void ProcessRecord()
        {
            PsSpecCmdletService.PsSpecHistoryEntry[] entries = 
                this.Service.GetPsSpecHistory(id, ChangeType.ToString(), from, to);

            bool resultFound = false;
            foreach (PsSpecCmdletService.PsSpecHistoryEntry entry in entries)
            {
                resultFound = true;
                WriteObject(new PsSpecHistoryEntry(
                    entry.Id, entry.ChangeType.ToString(), entry.LastUpdated, entry.ChangedBy, entry.ProjectId, 
                    entry.ProjectName, entry.CmdletId, entry.CmdletName, entry.ParameterId, 
                    entry.ParameterName, entry.Action.ToString(), entry.PropertyName, entry.OldValue, entry.NewValue,
                    entry.Notes));
            }

            // Generate an error if no items found, and no globbing was done
            if (
                (!resultFound) && 
                ( (From != DateTime.MinValue) || (To != DateTime.MaxValue)))
            {
                WriteError(new ErrorRecord(
                    new Exception(resources.HistoryEntryNotFound), 
                    "HISTORY_ENTRY_NOT_FOUND",
                    ErrorCategory.ObjectNotFound,
                    null));
            }
        }
    }
}